<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch">
      <!-- 所属部门、医生姓名、放号日期、放号时段 -->
      <el-form-item label="所属部门" prop="deptName">
        <el-input v-model="queryParams.deptName" placeholder="请输入所属部门" clearable style="width: 200px"
          @keyup.enter="handleQuery" />
      </el-form-item>

      <el-form-item label="医生姓名" prop="employeeName">
        <el-input v-model="queryParams.employeeName" placeholder="请输入医生姓名" clearable style="width: 200px"
          @keyup.enter="handleQuery" />
      </el-form-item>


      <el-form-item label="放号日期" prop="date">
        <el-date-picker placeholder="请选择放号日期" v-model="queryParams.date" value-format="YYYY-MM-DD" type="date" />
      </el-form-item>

      <el-form-item label="放号时段" prop="period">
        <el-select v-model="queryParams.period" placeholder="请选择放号时段" clearable style="width: 200px">
          <el-option v-for="(item, key) in datemap" :key="key" :label="item" :value="key" />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" icon="Plus" @click="handleAdd">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" icon="Edit" :disabled="single" @click="handleUpdate">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" icon="Delete" :disabled="multiple" @click="handleDelete">删除</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row> -->

    <el-table v-loading="loading" :data="noticeList" @selection-change="handleSelectionChange">
      <!-- <el-table-column type="selection" width="55" align="center" /> -->

      <!-- 
      序号、所属部门、医生姓名、医生头像、放号日期、
      放号时段、放号数量、预约人数、预约详情、
      医生电话、创建时间、状态、操作人员、操作
      -->
      <el-table-column label="序号" align="center" type="index" width="100">
        <template #default="{ $index }">
          {{ (queryParams.currentPage - 1) * queryParams.pageSize + $index + 1 }}
        </template>
      </el-table-column>

      <el-table-column label="所属部门" align="center" prop="deptName" />
      <el-table-column label="医生姓名" align="center" prop="employeeName" />
      <el-table-column label="医生头像" width="100" align="center" prop="employeeAvatar">
        <template #default="scope">
          <el-image style="width: 50px; height: 50px;" :src="scope.row.employeeAvatar"
            :preview-src-list="[scope.row.employeeAvatar]" preview-teleported>
          </el-image>
        </template>
      </el-table-column>

      <el-table-column label="放号日期" align="center" prop="date" />
      <el-table-column label="放号时段" align="center" prop="period">
        <template #default="scope">
          <span>{{ datemap[scope.row.period] || 100 }}</span>
        </template>
      </el-table-column>
      <el-table-column label="放号数量" align="center" prop="count" />
      <el-table-column label="预约人数" align="center" prop="appointmentCount" />
      <el-table-column label="预约详情" align="center" prop="appointmentDetail">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleDuser(scope.row)">预约详情 </el-button>
        </template>
      </el-table-column>
      <el-table-column label="医生电话" align="center" prop="phone" />
      <el-table-column label="创建时间" align="center" prop="createTime" />
      <el-table-column label="状态" align="center" prop="status">
        <template #default="scope">
          正常
        </template>
      </el-table-column>
      <el-table-column label="操作人员" align="center" prop="updateUserName" />


      <!-- class-name="small-padding fixed-width" -->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="240">
        <template #default="scope">
          <!-- /backAdmin/shiftApi/appointment/user/list 预约用户列表 -->
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.currentPage"
      v-model:limit="queryParams.pageSize" @pagination="getList" />

    <!-- 预约用户列表 -->
    <el-dialog :title="title" v-model="open" width="780px" append-to-body>
      <el-table :data="gridData">
        <el-table-column property="avatar" label="照片">
          <template #default="scope">
            <img width="50" height="50" :src="scope.row.avatar" alt="">
          </template>
        </el-table-column>
        <el-table-column property="phone" label="姓名" />
        <el-table-column property="name" label="手机号码" />
      </el-table>
    </el-dialog>

    <!-- 修改 -->
    <el-dialog title="修改放号" v-model="openUpdate" width="780px" append-to-body>
      <el-form-item label="修改放号">
        <el-input v-model="form.count" placeholder="请输入放号编号" clearable />
      </el-form-item>
      <template #footer>
        <el-button type="primary" @click="submitForm">提交</el-button>
        <el-button @click="cancel">取消</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Notice">
import datemap from './datemap.json'

import {
  listUpper,
  delUpper,
  updateUpper,
  getNotice,
  addNotice,
  updateNotice,
  shiftApiAppointmentUserList
} from "@/api/backShift/backShift";

const { proxy } = getCurrentInstance();
const { sys_notice_status, sys_notice_type } = proxy.useDict(
  "sys_notice_status",
  "sys_notice_type"
);

const noticeList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    currentPage: 1,
    pageSize: 10,
    employeeName: undefined,
    date: undefined,
    status: undefined,
  },
  rules: {},
});
const { queryParams, form, rules } = toRefs(data);

/** 预约用户列表 */
const gridData = ref([])
const handleDuser = async (row) => {
  title.value = "预约用户列表";
  open.value = true;
  let employeeShiftDetailId = row.employeeShiftDetailId
  // 预约用户列表
  gridData.value = await shiftApiAppointmentUserList(employeeShiftDetailId)
}


/** 查询放号列表 */
function getList() {
  loading.value = true;
  console.log(queryParams.value, "queryParams.value");

  listUpper(queryParams.value).then((response) => {
    noticeList.value = response.records;
    total.value = response.total;
    loading.value = false;
  });
}

/** 取消按钮 */
function cancel() {
  open.value = false;
  openUpdate.value = false;
  reset();
}

/** 表单重置 */
function reset() {
  form.value = {
    employeeShiftDetailId: undefined,
    employeeName: undefined,
    noticeType: undefined,
    noticeContent: undefined,
    status: "0",
  };
  proxy.resetForm("noticeRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.currentPage = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

/** 多选框选中数据 */
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.employeeShiftDetailId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加放号";
}

// 修改 
const openUpdate = ref(false)
/**修改按钮操作 */
function handleUpdate(row) {
  reset();
  console.log(row, "row");
  form.value.count = row.count;
  form.value.employeeShiftDetailId = row.employeeShiftDetailId;
  openUpdate.value = true;
  return
  const employeeShiftDetailId = row.employeeShiftDetailId || ids.value;
  getNotice(employeeShiftDetailId).then((response) => {
    form.value = response;
    openUpdate.value = true;
    title.value = "修改放号";
  });
}

/** 提交按钮 */
function submitForm() {


  if (form.value.employeeShiftDetailId != undefined) {
    console.log(form.value, "form.value changhe");
    updateUpper(form.value).then((response) => {
      proxy.$modal.msgSuccess("修改成功");
      openUpdate.value = false;
      getList();
    });
  } else {
    addNotice(form.value).then((response) => {
      proxy.$modal.msgSuccess("新增成功");
      open.value = false;
      getList();
    });
  }
}

/** 删除按钮操作 */
function handleDelete(row) {
  const employeeShiftDetailIds = row.employeeShiftDetailId || ids.value;
  proxy.$modal
    .confirm('是否确认删除放号编号为"' + employeeShiftDetailIds + '"的数据项？')
    .then(function () {
      return delUpper(employeeShiftDetailIds);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => { });
}

getList();
</script>